package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/shop")
@Api(tags = "后台-商铺管理")
@Slf4j
public class AdminShopController {

    public static final String ShopKey = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置商铺状态
     * @param status 1-营业中 2-打烊了
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation(value = "设置商铺状态")
    public Result SetStatus(@PathVariable("status") Integer status) {
        log.info("设置商铺状态：{}", status == 1 ? "营业中" : "打烊了");

        //设置商铺状态
        redisTemplate.opsForValue().set(ShopKey, status);

        return Result.success();
    }

    /**
     * 查询商铺状态
     * @return
     */
    @GetMapping("/status")
    @ApiOperation(value = "查询商铺状态")
    public Result<Integer> GetStatus() {

        //查询商铺状态
        Integer status = (Integer) redisTemplate.opsForValue().get(ShopKey);

        log.info("查询商铺状态：{}",status==1 ? "营业中" : "打烊了");

        return Result.success(status);
    }
}
